//
//  JJQiqiuController.m
//  MyAnimations
//
//  Created by jxf on 15/12/23.
//  Copyright © 2015年 JJ.sevn. All rights reserved.
//

#import "JJQiqiuController.h"
#import "JJPublicMacro.h"

@interface JJQiqiuController ()
@property(nonatomic, weak) CALayer * ballon;
@end
@implementation JJQiqiuController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupLabel];
    [self setupBallon];
}

- (void)setupLabel
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenW * 0.5 - 90, 200, 180, 50)];
    [self.view addSubview:label];
    label.text = @"摇啊摇摇到外婆桥";
    label.textColor = [UIColor blackColor];
    label.font = [UIFont boldSystemFontOfSize:20];
}

- (void)setupBallon
{
    CALayer *ballon = [CALayer layer];
    [self.view.layer insertSublayer:ballon atIndex:1];
    ballon.frame = CGRectMake(-100, 0, 100, 130);
    ballon.contents = (id)[UIImage imageNamed:@"balloon"].CGImage;
    self.ballon = ballon;
}

- (void)animate
{
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    anim.values = @[
                    [NSValue valueWithCGPoint:CGPointMake(-100, 0)],
                    [NSValue valueWithCGPoint:CGPointMake(500, 150)],
                    [NSValue valueWithCGPoint:CGPointMake(-50, 300)],
                    [NSValue valueWithCGPoint:CGPointMake(500, 650)]
                    ];
    anim.duration = 15.0;
    anim.repeatCount = MAXFLOAT;
    [self.ballon addAnimation:anim forKey:nil];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self animate];
}
- (IBAction)backHome:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
